package com.xuyang.server;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xuyang.utils.MySqlHelper;

/**
 * Servlet implementation class BookManageServlet
 */
public class BookManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1 添加图书  2归还图书 3挂失图书
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Type", "charset=utf-8");

		int flag = Integer.valueOf(request.getParameter("flag"));
		switch (flag) {
		case 1:
			String name=request.getParameter("name");
			String category=request.getParameter("category");
			String writer=request.getParameter("writer");
			String press=request.getParameter("press");
			String languages=request.getParameter("languages");
			String introduce=request.getParameter("introduce");
			String remarks=request.getParameter("remarks");
			name=new String(name.getBytes("ISO-8859-1"),"UTF-8");
			name=URLDecoder.decode(name);
			category=new String(category.getBytes("ISO-8859-1"),"UTF-8");
			category=URLDecoder.decode(category);
			writer=new String(writer.getBytes("ISO-8859-1"),"UTF-8");
			writer=URLDecoder.decode(writer);
			press=new String(press.getBytes("ISO-8859-1"),"UTF-8");
			press=URLDecoder.decode(press);
			languages=new String(languages.getBytes("ISO-8859-1"),"UTF-8");
			languages=URLDecoder.decode(languages);
			introduce=new String(introduce.getBytes("ISO-8859-1"),"UTF-8");
			introduce=URLDecoder.decode(introduce);
			remarks=new String(remarks.getBytes("ISO-8859-1"),"UTF-8");
			remarks=URLDecoder.decode(remarks);
			
			boolean bo=MySqlHelper.getInstance().update("insert into bookstacks(name,category,writer,press,languages,introduce,remarks) values(?,?,?,?,?,?,?)",
					new Object[]{name,category,writer,press,languages,introduce,remarks});
			if(bo){
				response.getWriter().write("ok");
			}else {
				response.getWriter().write("error");
			}
			break;
		case 2:
			String id=request.getParameter("bookId");
			MySqlHelper.getInstance().update("update bookborrow set returned=1 where bookId=?", new Object[]{id});
			bo=MySqlHelper.getInstance().update("update bookstacks set state=1 where id=?", new Object[]{id});
			if(bo){
				response.getWriter().write("ok");
			}else {
				response.getWriter().write("error");
			}
			break;
		case 3:
			id=request.getParameter("bookId");
			//图书挂失将图书从用户当前借阅中移除,然后删了这本书
			MySqlHelper.getInstance().update("update bookborrow set returned=1 where bookId=?", new Object[]{id});
			bo=MySqlHelper.getInstance().update("delete from bookstacks where id=?", new Object[]{id});
			if(bo){
				response.getWriter().write("ok");
			}else {
				response.getWriter().write("error");
			}
			break;
		default:
			break;
		}
	}

}
